package com.managementSystem.client.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.managementSystem.client.StaffBeanServiceAsync;
import com.managementSystem.client.event.RegisterStaff;
import com.managementSystem.client.presenter.MainMenuPresenter.Display;
import com.managementSystem.shared.StaffBean;

public class MainMenuPresenter implements Presenter  {
	StaffBean currentUser;
	public interface Display {
		HasClickHandlers getApproveButton();
		HasClickHandlers getSearchButton();
		HasClickHandlers getProfileButton();
		Widget asWidget();
	}
	private final StaffBeanServiceAsync rpcService;
	private final HandlerManager eventBus;
	private final Display display;

	public MainMenuPresenter(StaffBeanServiceAsync rpcService, HandlerManager eventBus, Display view) {
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = view;
	}
	
	public void bind() {
		display.getApproveButton().addClickHandler(new ClickHandler() {   
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new RegisterStaff());
			}
		});
		display.getProfileButton().addClickHandler(new ClickHandler() {   
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new RegisterStaff());
			}
		});
		display.getSearchButton().addClickHandler(new ClickHandler() {   
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new RegisterStaff());
			}
		});
	}
	@Override
	public void go(HasWidgets container) {
		bind();
		container.clear();
		container.add(display.asWidget());
		welcome();
	}

	private void welcome() {
		currentUser=new StaffBean();
		rpcService.getSessionStaff("user", new AsyncCallback<StaffBean>() {
			public void onSuccess(StaffBean result) {
				currentUser=result;
			}
			public void onFailure(Throwable caught) {
				Window.alert("Cannot get User");
			}
		});
		
	}

}
